package com.ezcoding.service;

import com.ezcoding.dao.MessageDao;
import com.ezcoding.model.Message;
import com.ezcoding.utils.StatusUtil;
import com.ezcoding.utils.Tools;

import java.util.*;

public class MessageService {

    private static MessageService instance = new MessageService();

    public static MessageService getInstance() {
        return instance;
    }

    public Map<String, Object> getMessages(String username) {
        if (Tools.checkParaIsInValidate(username)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        List<Message> messages = MessageDao.dao.getMessages(username);
        Collections.reverse(messages);

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        data.put("messages", messages);
        res.put("data", data);

        return StatusUtil.getSuccessStatusWithMessage(res, "获取消息成功！");
    }

    public Map<String, Object> getImportMessage(String username, int ticketAdjustType, int type) {
        if (Tools.checkParaIsInValidate(username)) {
            return StatusUtil.getFailedStatusWithInvalidParaMessage();
        }

        List<Message> messages;
        if (type == Message.MESSAGE_TYPE_TICKET_ADJUST) {
            List<Message> tempMessages = MessageDao.dao.getImportMessages(username, type);
            messages = new ArrayList<>();
            for (Message message : tempMessages) {
                switch (ticketAdjustType) {
                    case 0://ticketOut
                        if (message.getTicketIn() != 0) {
                            messages.add(message);
                        }
                        break;
                    case 1://moneyOut
                        if (message.getMoneyOut() != 0) {
                            messages.add(message);
                        }
                        break;
                    case 2://ticketIn
                        if (message.getTicketOut() != 0) {
                            messages.add(message);
                        }
                        break;
                    case 3://moneyIn
                        if (message.getMoneyIn() != 0) {
                            messages.add(message);
                        }
                        break;
                }
            }
        } else {
            messages = MessageDao.dao.getImportMessages(username, type);
        }

        if (messages.size() == 0) {
            return StatusUtil.getFailedStatusWithMessage("无相应记录！");
        }

        Collections.reverse(messages);

        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        data.put("importMessages", messages);
        res.put("data", data);

        return StatusUtil.getSuccessStatusWithMessage(res, "获取导入消息成功！");
    }
}
